#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QMessageBox>
#include <QFileDialog>
#include <QDebug>
#include <QTime>
#include <QDir>
#include <QFile>
#include <QLabel>
#include <QPen>
#include <QPainter>
#include <QRect>
#include <QImage>

#pragma execution_character_set("utf-8")

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    file_open = 0;
    file_new = 0;
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_actionopen_file_triggered()         /*打开文件*/
{
    ui ->  canvas -> clear();     /*清除label内容*/
    QString fileName = QFileDialog::getOpenFileName(this, tr("open image file"),
                    "./", tr("Image files(*.bmp *.jpg *.pbm *.pgm *.png *.ppm *.xbm *.xpm);;All files (*.*)"));


        if(fileName.isEmpty()){
            QMessageBox::warning(this,"Warning","无法打开图片");
            return;
        }
        else{
            QImage img(fileName);
            img = img.scaled(ui-> canvas ->width(),ui-> canvas ->height(),Qt::KeepAspectRatio);/*图像自适应大小，不变形*/
            ui -> canvas -> setPixmap(QPixmap::fromImage(img));          /*打开文件*/
            if(img.isNull()){
                QMessageBox::information(this,"Tip","无法打开文件");
            }
            else{
                file_open = 1;      /*标记，已保存文件*/
                Img = img;
            }
        }

}

void MainWindow::on_actionnew_file_triggered()          /*新建文件*/
{
    ui -> canvas -> clear();         /*清楚原文件内容*/
    file_new = 1;               /*表示该文件为新建文件*/
    file_open = 1;              /*标记，已打开文件*/
}

void MainWindow::on_actionsave_file_triggered()
{
    if(file_new){
        MainWindow::on_actionsave_as_file_triggered();      /*新建文件按照另存为处理*/
    }
    else{
        if(file_open == 1){
            QString filePath;
            QDir dir;
            filePath = dir.currentPath();   /*获取当前文件位置*/

            if(filePath.isEmpty()){
                QMessageBox::information(this,"Warning","保存失败");
            }
            else{
                QImage images;
                images = Img;           /*获取加载图片*/
                images.save(filePath);  /*保存QImage*/
                QMessageBox::information(this,"Tip","保存成功");
            }
        }
    }
}

void MainWindow::on_actionsave_as_file_triggered()      /*文件另存为*/
{
    if(file_open){         /*说明有文件加载，不然不需要保存*/
            QString filePath;       /*保存路径*/
            QDateTime time = QDateTime::currentDateTime();      /*获取当前时间*/
            QString str = time.toString("yyyyMMdd_hhmmss");     /*设置显示格式*/
            filePath = QFileDialog::getExistingDirectory(this,"");      /*获取文件路径*/
            if(filePath.isEmpty())
            {
                QMessageBox::information(this,"Warning","保存失败");
            }
            else{
                QImage images;
                images= Img;

                QString savePath = QString("%1/保存实验图%2.jpg").arg(filePath).arg(str);  /*设置文件保存路径与文件名*/
                images.save(savePath);
                QMessageBox::information(this,"Tip","保存成功");
            }
        }
        else
        {
            QMessageBox::information(this,"Warning","保存失败");
        }
}
